package com.aeasycredit.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpSocketServer {

	public static void main1(String[] args) throws IOException, InterruptedException {
	      ServerSocket serverSocket = new ServerSocket();
	        // 绑定端口
	        serverSocket.bind(new InetSocketAddress(26001));
	        System.out.println("服务器等待连接...  127.0.0.1:26001");
	        // 阻塞，等待客户机连接
	        Socket clientSocket = serverSocket.accept();
	        InetSocketAddress clientAddress = (InetSocketAddress) clientSocket.getRemoteSocketAddress();
	        System.out.println("接收到客户端连接" + clientAddress.getHostString() + ":" + clientAddress.getPort());

	        OutputStream out = clientSocket.getOutputStream();
	        InputStream in = clientSocket.getInputStream();
	        byte[] readBytes = new byte[1024];
	        // 阻塞，等待读取客户机数据
	        while (in.read(readBytes) != -1) {
	            System.out.println(new String(readBytes));
	            // 阻塞，代表服务器业务处理
	            Thread.sleep(200);
	            // 服务器向客户端回写数据
	            System.out.println("1111111111111");
	            out.write(String.valueOf(System.currentTimeMillis()).getBytes());
	            // 结尾回写回车+换行
	            System.out.println("22222222222222");
	            out.write("\r\n".getBytes());
	            // 手动刷新输出流
	            System.out.println("3333333333333333");
	            out.flush();
	            System.out.println("444444444444444");
	        }
	        // 关闭连接
	        clientSocket.close();
	        serverSocket.close();

	}
	
	public static void main(String[] args) throws IOException, InterruptedException {
	      ServerSocket serverSocket = new ServerSocket();
	        // 绑定端口
	        serverSocket.bind(new InetSocketAddress(26001));
	        System.out.println("服务器等待连接...  127.0.0.1:26001");
	        // 阻塞，等待客户机连接
	        Socket clientSocket = serverSocket.accept();
	        InetSocketAddress clientAddress = (InetSocketAddress) clientSocket.getRemoteSocketAddress();
	        System.out.println("接收到客户端连接" + clientAddress.getHostString() + ":" + clientAddress.getPort());

	        OutputStream os = clientSocket.getOutputStream();
	        InputStream in = clientSocket.getInputStream();
	        
	        System.out.println("1111111111111");
	        BufferedReader br = new BufferedReader(new InputStreamReader(in));
	        StringBuilder str = new StringBuilder();
	        String data = null;
	        while((data = br.readLine())!=null) {
	            str.append(data).append("\n");
	        }
	        System.out.println("2222222222222222" + str);
	        clientSocket.shutdownInput();
	        System.out.println("=====");
	        
	        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
	        bw.write("hello\r\n");
	        bw.flush();
	        System.out.println("333333333333333");
	        clientSocket.shutdownOutput();
	}

}
